package com.wc.算法提高课.B第二章_搜索.双向DFS.送礼物;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/9/30 13:50
 * @description https://www.acwing.com/problem/content/173/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50;
    static int[] w = new int[N];
    static int[] weight = new int[1 << 25];
    static int n, m, cnt = 0, k, ans = 0;

    public static void main(String[] args) {
        m = sc.nextInt();
        n = sc.nextInt();
        for (int i = 0; i < n; i++) w[i] = sc.nextInt();
        Arrays.sort(w, 0, n);
        for (int i = 0, j = n - 1; i < j; i++, j--) {
            int tmp = w[i];
            w[i] = w[j];
            w[j] = tmp;
        }
        k = n / 2 + 2;
        // dfs一半
        dfs1(0, 0);
        Arrays.sort(weight, 0, cnt);
        // 去重
        int j = 0;
        for (int i = 0; i < cnt; i++) {
            if (weight[i] != weight[j]) weight[++j] = weight[i];
        }
        cnt = j + 1;
        // dfs另一半打表，二分查询
        dfs2(k, 0);
        out.println(ans);
        out.flush();
    }


    static void dfs1(int u, int s) {
        if (u >= k) {
            weight[cnt++] = s;
            return;
        }
        dfs1(u + 1, s);
        if ((long) s + w[u] <= m) dfs1(u + 1, s + w[u]);
    }

    static void dfs2(int u, int s) {
        if (u >= n) {
            int l = 0, r = cnt - 1;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if ((long)weight[mid] + s <= m) l = mid;
                else r = mid - 1;
            }
            ans = Math.max(weight[l] + s, ans);
            return;
        }
        dfs2(u + 1, s);
        if ((long) s + w[u] <= m) dfs2(u + 1, s + w[u]);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
